const path = require("path");
const { BrowserWindow, ipcMain, Menu, globalShortcut, Notification  } = require("electron");
const dialog = require(path.join(__dirname, "../dialog/dialog"));
const store = require(path.join(__dirname, "../../store/store"));
const { setMenu, serveMenu } = require(path.join(__dirname, "./meau.js"));
const addChannel = require(path.join(__dirname, "../addChannel/addChannel"));
const passwordInput = require(path.join(
  __dirname,
  "../passwordInput/passwordInput"
));
const CreateAddServeWindow = require(path.join(
  __dirname,
  "../addServe/addServe"
));

let win = null;
module.exports = {
   createWindow: () => {
    win = new BrowserWindow({
      width: 800,
      height: 600,
      minwidth: 400,
      minheight: 300,
      webPreferences: {
        preload: path.join(__dirname, "../preload.js"),
      },
    });
    setMenu(win);
    win.setIcon(path.join(__dirname, "../../static/img/favicon.ico"));
    win.loadFile(path.join(__dirname, "../../page/index/index.html"));
    win.on('close', (e) => {
      e.preventDefault();
      new Notification({
        title: 'WeTalk',
        body: 'WeTalk已隐藏至托盘'
      }).show()
      win.hide()
    })
  },
  getWindow: () => {
    return win; // 提供一个方法来获取 win 实例
  },
};


ipcMain.handle("data-init", () => {
  return init();
});
function init() {
  store.get("voice") &&
    !globalShortcut.isRegistered(
      `CommandOrControl + Alt + ${store.get("voice")}`
    ) &&
    globalShortcut.register(
      `CommandOrControl + Alt + ${store.get("voice")}`,
      () => {
        win.webContents.send("voice-swich");
      }
    );
  store.get("mic") &&
    !globalShortcut.isRegistered(
      `CommandOrControl + Alt + ${store.get("mic")}`
    ) &&
    globalShortcut.register(
      `CommandOrControl + Alt + ${store.get("mic")}`,
      () => {
        win.webContents.send("mic-swich");
      }
    );
  return {
    username: store.has("username") ? store.get("username") : null,
    isOpenVoice: store.has("isOpenVoice") ? store.get("isOpenVoice") : null,
    totalVolume: store.has("totalVolume") ? store.get("totalVolume") : 1,
  };
}
ipcMain.handle("updataServerList", async () => {
  serverList = store.get("serverList");
  return serverList;
});

ipcMain.on("connctError", async (event, obj) => {
  dialog(win, ...obj);
});

ipcMain.handle("getServe", () => {
  return store.get("serverList");
});
ipcMain.on("addServer", (event) => {
  const AddServeWindow = new CreateAddServeWindow(
    BrowserWindow.fromWebContents(event.sender)
  );
  AddServeWindow.show();
});
ipcMain.on("addChannel", (event) => {
  addChannel
    .createAddChannelWin(BrowserWindow.fromWebContents(event.sender))
    .show();
});
ipcMain.on("passwordInput", (event) => {
  passwordInput
    .createInputWindow(BrowserWindow.fromWebContents(event.sender))
    .show();
});
ipcMain.on("devidceGet", (event, template) => {
  template = template.map((device) => ({
    label: device.label,
    id: device.id,
    click: () => {
      // 发送选定的设备值回主进程
      event.sender.send("deviceSelected", device.id);
    },
  }));
  const menu = Menu.buildFromTemplate(template);
  menu.popup({ window: BrowserWindow.fromWebContents(event.sender) });
});
ipcMain.on("serveMenu", (event, mes) => {
  serveMenu(BrowserWindow.fromWebContents(event.sender), mes);
});

ipcMain.on("change-volume", (event, mes) => {
  store.set("totalVolume", mes);
});
